let requestUrl = null;
let relateArray = ["不关联", "关联"];
let typeArray = [null, "角色等级", "宠物等级"];

function addHandler() {
	requestUrl = "/fabao_category/insert";

	$("#fabao_dialog").dialog("open");
}

function editHandler() {
	let rowData = $("#fabao_list").datagrid("getSelected");
	
	if (rowData) {
		requestUrl = "/fabao_category/updateById";

		$("#id").val(rowData.id);
		$("#name").textbox("setValue", rowData.name);
		$("#note").textbox("setValue", rowData.note);
		$("#type").combobox("setValue", rowData.type);
		$("#schoolId").combobox("setValue", rowData.schoolId);
		$("#relateType").combobox("setValue", rowData.relateType);
		$("#relateGrade").combobox("setValue", rowData.relateGrade);

		$("#fabao_dialog").dialog("open");
	} else {
		alertMsg("请选择要修改的记录！", "warning");
	}
}

function imageHandler() {
	let rowData = $("#fabao_list").datagrid("getSelected");

	if (rowData) {
		requestUrl = "/fabao_category/updateById";

		$("#categoryId").val(rowData.id);
		$("#img").attr("src", rowData.image);

		$("#upload_dialog").dialog("open");
	} else {
		alertMsg("请选择要修改的记录！", "warning");
	}
}

$(document).ready(function() {

	$("#_type_").combobox({
		url: "/fabao_type/selectAll",
		valueField: "id",
		textField: "name",
		width: 150,
		panelHeight: "auto",
		prompt: "-选择法宝类型-",
	});

	$("#_name_").textbox({
		prompt: "请输入法宝名称"
	});

	// 门派
	$("#school_id").combobox({
		url: "/school/selectAll",
		valueField: "id",
		textField: "name",
		width: 150,
		panelHeight: "auto",
		prompt: "-选择门派-",
		formatter: function(row) {
			return "<img height='20' src='" + row.image + "' />&nbsp;" + row.name;
		}
	});

	$("#search").linkbutton({
		iconCls: "icon-search"
	}).click(function() {
		let name = $("#_name_").textbox("getValue");
		let type = $("#_type_").combobox("getValue");
		let schoolId = $("#school_id").combobox("getValue");

		$("#fabao_list").datagrid("reload", {
			schoolId: schoolId,
			type: type,
			name: name
		});
	});

	$("#clear").linkbutton({
		iconCls: "icon-delete"
	}).click(function() {
		$("#search_form").form("clear");
	});

	/******************************************************************************************************/

	$("#type").combobox({
		url: "/fabao_type/selectAll",
		valueField: "id",
		textField: "name",
		width: 120,
		required: true,
		panelHeight: "auto"
	});

	$("#name").textbox({
		width: 120,
		required: true
	});

	fetchSelect("#relateGrade", relateArray, {
		width: 120,
		required: false
	});

	fetchSelect("#relateType", typeArray, {
		width: 120,
		required: false
	});

	$("#schoolId").combobox({
		url: "/school/selectAll",
		valueField: "id",
		textField: "name",
		width: 120,
		panelHeight: "auto",
		formatter: function(row) {
			return "<img width='20' src='" + row.image + "' />&nbsp;" + row.name;
		}
	});

	$("#note").textbox({
		width: 320,
		height: 80,
		required: true,
		multiline: true
	});

	$("#fabao_dialog").dialog({
		title: "法宝信息",
		width: 440,
		buttons: [{
			iconCls: "icon-save",
			text: "保存",
			handler: function() {
				let selector = "#fabao_form";

				checkForm(selector, function () {
					let data = $(selector).serialize();

					ajaxPost(requestUrl, data, function(resp) {
						showMsg(resp.message);

						$(selector).form("clear");
						$("#fabao_dialog").dialog("close");
						$("#fabao_list").datagrid("reload");
					}, error);
				});
			}
		}, {
			iconCls: "icon-cancel",
			text: "取消",
			handler: function() {
				$("#fabao_dialog").dialog("close");
				$("#fabao_form").form("clear");
			}
		}]
	});

	/******************************************************************************************************/

	fetchUploadFilebox("#fabao_list", "/fabao_category/upload");

	fetchUploadDialog("/fabao_category/updateImageById", function () {
		$("#fabao_list").datagrid("reload");
	});

	/******************************************************************************************************/

	/**
	 * 表格体的高度
	 * @type {number}
	 */
	let heigt_body = heigt_tr_with_image * 10;
	/**
	 * 表格高度
	 * @type {number}
	 */
	let height_table = heigt_tool + heigt_header + heigt_body + heigt_footer + heigt_scoll_bar + 2;

	$("#fabao_list").datagrid({
		url: "/fabao_category/selectByPage",
		height: height_table,
		fitColumns: true,
		rownumbers: true,
		pagination: true,
		toolbar: [{
			iconCls: "icon-add",
			text: "添加",
			handler: function() {
				addHandler()
			}
		}, "-", {
			iconCls: "icon-edit",
			text: "修改",
			handler: function() {
				editHandler();
			}
		}, "-", imageUpdateButton],
		columns: [[
			{field: "name", title: "法宝名称", align: "center", width: 100},
			{field: "type", title: "法宝类型", align: "center", width: 100,
				formatter: function(value, rowData, rowIndex) {
					ajaxGet("/fabao_type/selectById", {
						id: value
					}, function (resp) {
						let data = resp.data;

						$("#type_" + rowIndex).html(data.name);
					}, error);

					return "<div id='type_" + rowIndex + "'></div>";
				}
			},
			{field: "image", title: "图片", align: "center", width: 40
				, formatter: function(value) {
					return "<img height='" + size + "' src='" + value + "' />";
				}
			},
			{field: "note", title: "法宝信息", align: "center", width: 400,
				formatter: function(value) {
					return "<div class='ell'>" + value + "</div>";
				}
			},
			{field: "schoolId", title: "门派", align: "center", width: 60
				, formatter: function(value, rowData, rowIndex) {
					if (value) {
						ajaxGet("/school/selectById", {
							id: value
						}, function(resp) {
							let data = resp.data;

							$("#school_" + rowIndex).attr("src", data.image)
								.attr("title", data.name);
						}, error);

						return "<img height='" + size + "' id='school_" + rowIndex + "' />";
					} else {
						return "<span>/</span>";
					}
				}
			},
		]]
	});

});